<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './suppiler.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicTree } from '/@/components/Tree';
  import { CropperAvatar } from '/@/components/Cropper';
  import {
    updateSuppilerApi,
    addSuppilerApi
  } from '/@/api/modules/business/product/suppiler';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getDictEntryWithListByDictCodeApi } from '/@/api/modules/system/dictEntry';
  export default defineComponent({
    name: 'SuppilerDrawer',
    components: { BasicDrawer, BasicForm, BasicTree, CropperAvatar },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);

      const rowId = ref();

      const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
        labelWidth: 120,
        // baseColProps: { span: 24 },
        schemas: formSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const { createMessage } = useMessage();
      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        // let provincesOptions: any = [];
        // Object.keys(pcaa['86']).map(key => {
        //   provincesOptions.push({
        //     label: pcaa['86'][key],
        //     value: key,
        //   })
        // })
        // updateSchema([
        //   {
        //     field: 'province',
        //     // ...provinceComponentProps(provincesOptions)
        //     componentProps: {
        //       options: provincesOptions
        //     }
        //     // componentProps: ({ formModel, formActionType }) => {
        //     //   return {
        //     //     options: provincesOptions,
        //     //     onchange: (e: any) => {
        //     //       console.log(e)
        //     //       let citiesOptions: any = []
        //     //       Object.keys(pcaa[formModel.province]).map(key => {
        //     //         citiesOptions.push({ label: key, value: pcaa[formModel.province][key] })
        //     //       })
        //     //       formModel.city = undefined; //  reset city value
        //     //       const { updateSchema } = formActionType;
        //     //       updateSchema({
        //     //         field: 'city',
        //     //         componentProps: {
        //     //           options: citiesOptions,
        //     //         },
        //     //       });
        //     //     }
        //     //   }
        //     // },
        //   }
        // ])
        if (unref(isUpdate)) {
          rowId.value = data.record.id;
          // Object.keys(pcaa['86']).filter(key => pcaa['86'][key] == '北京市').map(key => console.log(key))
          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增物资来源' : '编辑物资来源'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setDrawerProps({ confirmLoading: true });
          !unref(isUpdate)
            ? await addSuppilerApi(values)
            : await updateSuppilerApi({ ...values, id: unref(rowId) });
          !unref(isUpdate) ? createMessage.success('新增成功') : createMessage.success('编辑成功');
          closeDrawer();
          emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }
      
      function doSomething(){  

      }

      return {
        registerDrawer,
        registerForm,
        getTitle,
        handleSubmit,
        doSomething,
      };
    },
  });
</script>
